// pages/registration/today/inputInfo/inputInfo.js
var util = require('../../../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hospital:'白泽第一中心医院',
    type: '普通号',
    "array": [],
    index: 0,
    isFa:["请选择号段","上午","下午"],
    index2:0,
    money: 10,
    keshi:"",
    haoduan:"",
    isShow:false,
    time: true
  },
  bindPickerChange: function(e) { 
    let that = this;
    console.log('picker发送选择改变，选的科室为：', e.detail.value)
    that.setData({
      index: e.detail.value,
      keshi: that.data.array[e.detail.value].deparName
    })
    console.log("选择的科室为",that.data.keshi,"id为：",that.data.index)
    if(that.data.index != 0 && that.data.index2 != 0 && that.data.time){
      that.setData({
        isShow: true
      })
        
    }else{
      that.setData({
        isShow: false
      })
    }
  },
  bindPickerChange2: function(e) {
    let that = this;
    console.log('picker发送选择改变，选择的号段为', that.data.isFa[e.detail.value])
    that.setData({
      index2: e.detail.value,
      haoduan: that.data.isFa[e.detail.value]
    })
    console.log("选择的号段为：",that.data.haoduan)
    if(that.data.index != 0 && that.data.index2 != 0 && that.data.time){
      wx.setStorageSync('haoduanStory', true)
      that.setData({
        isShow: true
      })
    }else{
      that.setData({
        isShow: false
      })
    }
    
  },

  look(){
    console.log("首次必看")
  },
  
  btnClick(){
    console.log("挂号")
    let yy = this.data.hospital;
    let ks = this.data.keshi;
    let id = this.data.index;
    let lx = this.data.type;
    let hd = this.data.haoduan;
    let je = this.data.money;
    let timeDay = util.tadayDate();
    //将页面的数据放在缓存中
    wx.setStorageSync('hospital', yy);
    wx.setStorageSync('keshi', ks)
    wx.setStorageSync('departmentId', id);
    wx.setStorageSync('type', lx);
    wx.setStorageSync('haoduan', hd);
    wx.setStorageSync('money', je);
    wx.setStorageSync('time', timeDay);
    //跳转页面到确认挂号信息页
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    wx.navigateTo({
      url: '/pages/registration/affirm/affirm',
    })
    
    console.log(ks,hd)
    
  },
  timeArray(){
    var date = new Date();
    var num = date.getHours() + (date.getMinutes()/60);
    console.log(num)
    if(num < 8 || num > 16){
      this.setData({
        time: false,
        isFa: ["请选择号段"]
      })
      wx.showToast({
        title: '非可挂号时间，不可挂号',
        icon: 'none',
        duration: 2000
      })   
    }else if(num >= 8 && num <= 11){
      this.setData({
        isFa: ["请选择号段","上午","下午"]
      })

    }else if(num > 11 && num <= 16 ){
      this.setData({
        isFa: ["请选择号段","下午"]
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.departments();
    this.timeArray();
    this.setData({
      index: 0,
      index2: 0,
      isShow: false

    })
    console.log("index:",this.data.index,"index2",this.data.index2,"isShow",this.data.isShow)
  },

  departments(){
    let that = this;
    wx.request({
      url: 'http://localhost:8060/user/showDepartmentsInfo',
      method: 'POST',
      header: { 'Content-Type': 'application/json' },
      success(res){  
        console.log("科室信息:",res.data.showInfo);

       var arr = new Array();
       //forEach循环，将对象数组遍历并赋值给一个新的list（"array"）
        res.data.showInfo.forEach(function(item, index) {
          let id = item.id;
          let deparName = item.deparName;
          arr[0] = {id:0,deparName:"请选择科室"};
          arr[index+1] = {id,deparName};    
        });
        //赋值
        console.log(arr)

        that.setData({
          "array": arr
        })
        console.log("科室信息:",that.data.array);
      }
    })
  }
  
})